class Player < ActiveRecord::Base
	has_many :cards, :dependent => :destroy
	has_many :bids, :dependent => :destroy
	has_one :partner, :class_name => 'Player', :foreign_key => :partner_id
	has_many :tricks, :foreign_key => 'player_0_id'
	has_many :tricks, :foreign_key => 'player_1_id'
	has_many :tricks, :foreign_key => 'player_2_id'
	has_many :tricks, :foreign_key => 'player_3_id'


	belongs_to :game
	def add_card(params)
		params[:player_id] = self.id
		Card.create params
	end
	def display
		result = "";
		clubs = self.cards.select{|c| c.suit == 'c'}.sort_by{|c| c.value}
		diamonds = self.cards.select{|c| c.suit == 'd'}.sort_by{|c| c.value}
		hearts = self.cards.select{|c| c.suit == 'h'}.sort_by{|c| c.value}
		spades = self.cards.select{|c| c.suit == 's'}.sort_by{|c| c.value}
		[clubs.size, diamonds.size, hearts.size, spades.size].max.times do |a|
			result << "#{clubs[a].nil? ? '--' : clubs[a].pp}\t"
			result << "#{diamonds[a].nil? ? '--' : diamonds[a].pp}\t"
			result << "#{hearts[a].nil? ? '--' : hearts[a].pp}\t"
			result << "#{spades[a].nil? ? '--' : spades[a].pp}\n"
		end
		return result
	end
end
